package com.i2863.tools.weixinmp.controller;


import com.i2863.tools.weixinmp.dao.mapper.VideoMapper;
import com.i2863.tools.weixinmp.domain.VideoInfo;
import com.i2863.tools.weixinmp.service.NewsService;
import com.i2863.tools.weixinmp.service.SiteAreaService;
import com.i2863.tools.weixinmp.service.WeixinService;
import com.i2863.tools.weixinmp.util.QiniuUpload;
import com.i2863.tools.weixinmp.util.UploadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by xuweiwei on 2017/5/4.
 */
@Controller
@RequestMapping("/")
public class UploadController {


    private static Logger log = LoggerFactory.getLogger(UploadController.class);
    @Autowired
    private WeixinService weixinService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private SiteAreaService siteAreaService;
    @Resource
    private VideoMapper videoMapper;

    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");




    @RequestMapping(value = "/file_upload")
    public String upload(
            @RequestParam(value ="duration",required = false) String duration,
            @RequestParam(value = "file", required = false)MultipartFile[] files,
            @RequestParam(value = "thumb", required = false)MultipartFile[] thumb,
                      @RequestParam(value = "source",required = true)String source,
                      @RequestParam(value = "title",required = true)String title,
                      @RequestParam(value = "other",required = false)String other,
                      @RequestParam(value = "url",required = false)String url,
                         Model model,
                      HttpServletRequest req, HttpServletResponse resp
    ) throws IOException {
        //解析来源
        String realSource = null;
        if("其他".equals(source)){
            realSource=other;
        }else{
            realSource = source;
        }
        //解析时长
        VideoInfo videoInfo = new VideoInfo();
        if(duration!=null && duration.length()>0){
            videoInfo.setDuration(duration);
        }
        //解析缩略图
        if(thumb!=null && thumb.length>0){
            String thumbUrl = UploadUtil.uploadImg(thumb[0].getOriginalFilename(),thumb[0].getBytes());
            videoInfo.setThumbPic(thumbUrl);
        }



        try {
            if (url != null && url.trim().length() > 0) {
                url = url.trim();
                if (url.contains("video.eastday.com")) {
                    String videoUrl = UploadUtil.getTouTiaoVideoUrl(url);
                    String qiniuUrl = UploadUtil.upload(videoUrl, url);
                    videoInfo.setSource("头条视频网");
                    videoInfo.setTitle(title);
                    videoInfo.setUrl(qiniuUrl);
                    videoInfo.setVideoTime(new Date());
                    videoMapper.save(videoInfo);
                    model.addAttribute("message","成功");
                    model.addAttribute("title",videoInfo.getTitle());
                    model.addAttribute("url",qiniuUrl);
                } else {
                    String qiniuUrl = UploadUtil.upload(url);
                    videoInfo.setSource(realSource);
                    videoInfo.setTitle(title);
                    videoInfo.setUrl(qiniuUrl);
                    videoInfo.setVideoTime(new Date());
                    videoMapper.save(videoInfo);
                    model.addAttribute("message","成功");
                    model.addAttribute("title",videoInfo.getTitle());
                    model.addAttribute("url",qiniuUrl);
                }

            } else {
                if (files.length > 0) {
                    String qiniuUrl = UploadUtil.upload(files[0].getBytes());
                    videoInfo.setSource(realSource);
                    videoInfo.setTitle(title);
                    videoInfo.setUrl(qiniuUrl);
                    videoInfo.setVideoTime(new Date());
                    videoMapper.save(videoInfo);
                    model.addAttribute("message","成功");
                    model.addAttribute("title",videoInfo.getTitle());
                    model.addAttribute("url",qiniuUrl);
                } else {
                    model.addAttribute("message","没有文件和url");
                }
            }

        }catch (Exception e){
            model.addAttribute("message","上传失败");
            log.error("上传失败",e);
        }

        return "message";
    }






}
